<!--
* description: 销退管理-销退申请新增
* author: ligy
* createdDate: 2020-11-17
-->
<template>
  <div class="app-container app-container-table">
    <el-dialog
      v-dialogDrag
      style="margin:15px"
      :visible="popupsVisible"
      :title="titleName"
      center
      width="50%"
      :append-to-body="true"
      :close-on-click-modal="false"
      @close="closeEdit"
    >
      <dialogHeader slot="title" :title="titleName" :style-type="$store.state.app.dialogType" />
      <div class="filter-container filter-params">
        <!-- 表单组件 -->
        <one-table-template
          ref="multipleTable"
          :dynamic-buttons="tableButtons"
          :dynamic-components="tableComponents"
          :dynamic-form-fields="formField"
          :callback-obj="callbackObj"
          :dynamic-is-init-table="false"
        />
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import { orgApis } from '@/api/apiList/org'
import OneTableTemplate from '@/components/templates/oneTable'
import dialogHeader from '@/components/dialogHeader'
import Vue from 'vue'
export default {
  components: {
    OneTableTemplate,
    dialogHeader
  },
  mixins: [oneTableWithViewTemplateMixins],
  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, 'supplierMessage-edit'], () => {
      next()
    })
  },
  props: {
    popupsVisible: {
      type: Boolean,
      default: false
    },
    dataForForm: {
      type: Object,
      default: () => {}
    },
    titleName: {
      type: String,
      default: ''
    }
  },
  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      listy: {},
      valid: true,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          clickEvent: () => {
            this.confirm()
          },
          text: '确认', // 查询
          name: 'save', // 按钮图标样式
          position: '',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          type: '',
          size: 'small',
          clickEvent: () => { this.closeEdit() },
          text: '取消',
          name: 'reset',
          position: '' // 取消
        }
      ],
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            // 显示组件
            {
              compKey: 'compKey1',
              labelName: '类型：',
              codeField: 'saleOrderType',
              component: () => import('@/components/org/commonSelect'),
              lookuptype: 'VE0031',
              isRequire: true,
              isMust: true,
              span: 8
            },
            {
              compKey: 'compKey2',
              labelName: '订单号：',
              codeField: 'associatedOrderNumber',
              // parentFileds: 'disabled:saleOrderType-associatedOrderNumber--false,value:saleOrderType',
              isMul: false,
              disabled: true,
              isRequire: true,
              component: () => import('./associatedOrderNumber'),
              type: 'inputText',
              isMust: true,
              span: 8
            },
            {
              compKey: 'compKey51',
              labelName: '退货金额：',
              codeField: 'enterpriseName',
              isMul: false,
              // disabled: true,
              isRequire: true,
              component: () => import('@/components/org/commonInput'),
              validrule: { maxlength: 11, isformat: /^([0-9]*)+(\.[0-9]{1,2})?$/ },
              type: 'inputText',
              isMust: true,
              span: 8
            },
            {
              compKey: 'compKey3',
              labelName: '退货原因：',
              codeField: 'saleOrderReason',
              textField: 'saleOrderReasonCn',
              lookuptype: 'VE0032',
              isMul: false,
              isRequire: true,
              component: () => import('@/components/org/commonSelect'),
              isMust: true,
              span: 24
            },
            {
              compKey: 'compKey4',
              labelName: '已收定金：',
              codeField: 'receivedDespoist',
              isShow: true,
              component: () => import('./weikuan'),
              isMust: true,
              span: 6
            },
            {
              compKey: 'compKey52',
              labelName: '已收尾款：',
              codeField: 'receivedEnd',
              isShow: true,
              component: () => import('./weikuan'),
              isMust: true,
              span: 7
            },
            {
              compKey: 'compKey5',
              labelName: '已收总金额：',
              codeField: 'receivedAmount',
              isShow: true,
              component: () => import('./weikuan'),
              isMust: true,
              span: 7
            }
          ],
      formField: {
        receivedDespoist: '',
        receivedAmount: '',
        receivedEnd: '',
        saleOrderType: '',
        saleOrderReason: '',
        saleOrderReasonCn: '',
        enterpriseName: '',
        UPDATE_CONTROL_ID: '',
        SALE_ORDER_ID: '',
        orderStatus: '',
        associatedOrderNumber: ''
      },
      // 是否使用刷新Key的方式刷新弹窗
      resetDialogKey: false,
      // 单表格模版页面组件回调事件
      callbackObj: {

      }
    }
  },
  methods: {
    // 确认
    confirm() {
      this.$utils.validataMoth(this, 'validpage')
      if (this.valid) {
        const loading = this.$loading({ text: '正在提交销退订单...' })
        this.$requestAPI({
          apiUrl: '/mp/e3sh/vesale/salertnmgr/salesReturnService/applySalesReturn.do',
          variables: { ...this.$refs.multipleTable.list, ...this.formField, orderDlrId: this.$refs.multipleTable.list.deliveryDlrId, saleOrderDId: this.$refs.multipleTable.list.saleOrderDId }
        }).then(res => {
          loading.close()
          this.$emit('updation')
          if (res.result === '1') {
            res.msg ? this.$message(res.msg) : this.$message('销退订单保存成功!')
            this.$emit('update:popupsVisible', false)
          } else {
            res.msg ? this.$message(res.msg) : this.$message('销退订单保存失败!')
          }
        }).catch(err => {
          loading.close()
          err.message ? this.$message(err.message) : this.$message(err.response.data.msg)
        })
      }
    },
    // 关闭弹窗
    closeEdit(type) {
      this.$emit('update:popupsVisible', false)
    }
  }
}
</script>
